﻿using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Microsoft.Office.Interop;
using WindowsFormsApplication1;

namespace office
{
    public partial class Formword : Form
    {
        private WordApp wordApp;

        public Formword()
        {
            InitializeComponent();
        }

        /// <summary>
        /// function
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string sourse = this.textBox1.Text.Trim();

                WordApp wordApp = new WordApp();
                wordApp.createDocument();

                writeFileName(sourse, wordApp);
                zhushi_function(sourse, wordApp);
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sourse"></param>
        /// <param name="wordApp"></param>
        private void writeFileName(string sourse, WordApp wordApp)
        {
            FileInfo f = new FileInfo(sourse);
            wordApp.insertTitle(f.Name, word1.Style.One);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sourse"></param>
        /// <param name="wordApp"></param>
        private void zhushi_function(string sourse, WordApp wordApp)
        {
            Encoding encode = Encoding.Default;
            System.IO.File.SetAttributes(sourse, System.IO.FileAttributes.Normal);
            FileStream fs = new FileStream(sourse, FileMode.Open);
            StreamReader read = new StreamReader(fs, encode);
            string temp = read.ReadLine();

            StringBuilder sb = new StringBuilder();
            while (temp != null)
            {
                temp = temp.TrimStart();
                string reg = @"\s+\w+\s*\(+";
                Regex r = new Regex(reg);

                //Thread.Sleep(10);
                if (!temp.Equals(""))
                {
                    if (temp.ToLower().IndexOf("function") != -1 && temp.Substring(0, 4).ToLower().Equals("func"))
                    {
                        Match m = r.Match(temp);

                        if (m.Value.ToString() != "")
                        {
                            temp = m.Value.ToString();

                            temp = temp.Substring(0, temp.Length - 1);
                            wordApp.insertTitle(temp, word1.Style.Three, Microsoft.Office.Interop.Word.WdColor.wdColorBlack);
                            wordApp.insertText(sb.ToString());

                            sb.Length = 0;
                        }
                    }

                    if (temp.Trim().Length > 2)
                    {
                        if (temp.TrimStart().Substring(0, 2).Equals("//"))
                        {
                            sb.Append(temp + "\n");
                        }
                    }
                }

                temp = read.ReadLine();
            }

            fs.Close();
        }

        /// <summary>
        /// liu lan js
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog2 = new OpenFileDialog();
            //openFileDialog2.Multiselect = true;
            openFileDialog2.RestoreDirectory = true;

            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = openFileDialog2.FileName;
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                zhuijia_js();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
        }

        /// <summary>
        ///
        /// </summary>
        private void zhuijia_js()
        {
            if (!textBox2.Text.Equals("") && !textBox1.Text.Equals(""))
            {
                wordApp.SetSelectionAtEndOfDocument();
                wordApp.insertText("");
                wordApp.insertText("");

                string sourse = textBox1.Text;
                System.IO.File.SetAttributes(sourse, System.IO.FileAttributes.Normal);

                zhushi_all(sourse);
            }
            else
            {
                MessageBox.Show("请输入文本框");
            }
        }

        /// <summary>
        ///
        /// </summary>
        private void zhuijia_cs()
        {
            if (!textBox4.Text.Equals("") && !textBox3.Text.Equals(""))
            {
                wordApp.SetSelectionAtEndOfDocument();
                wordApp.insertText("");
                wordApp.insertText("");

                string sourse = textBox4.Text;
                System.IO.File.SetAttributes(sourse, System.IO.FileAttributes.Normal);

                zhushi_cs(sourse);
            }
            else
            {
                MessageBox.Show("请输入文本框");
            }
        }

        private void zhushi_all(string sourse)
        {
            writeFileName(sourse, wordApp);

            wordApp.insertTitle("var", word1.Style.Two, Microsoft.Office.Interop.Word.WdColor.wdColorBlue);

            zhushi_var(sourse, wordApp);
            wordApp.insertText("");
            wordApp.insertText("");

            wordApp.insertTitle("function", word1.Style.Two, Microsoft.Office.Interop.Word.WdColor.wdColorBlue);
            zhushi_function(sourse, wordApp);
            wordApp.insertText("");
            wordApp.insertText("");
        }

        /// <summary>
        /// open
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.RestoreDirectory = true;

                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    this.textBox2.Text = openFileDialog1.FileName;
                    wordApp = null;
                    string filename = textBox2.Text;
                    wordApp = new WordApp();
                    wordApp.open(filename);
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                string sourse = this.textBox1.Text.Trim();

                VarCreate1(sourse);
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sou"></param>
        public void VarCreate1(string sou)
        {
            WordApp wordApp = new WordApp();
            wordApp.createDocument();

            FileInfo i = new FileInfo(sou);

            if (i.Extension.Equals(".js"))
            {
                writeFileName(i.FullName, wordApp);
                zhushi_var(i.FullName, wordApp);
            }
        }

        /// <summary>
        /// var
        /// </summary>
        /// <param name="sourse"></param>
        /// <param name="wordApp"></param>
        private void zhushi_var(string sourse, WordApp wordApp)
        {
            Encoding encode = Encoding.Default;
            System.IO.File.SetAttributes(sourse, System.IO.FileAttributes.Normal);
            using (FileStream fs = new FileStream(sourse, FileMode.Open))
            {
                StreamReader read = new StreamReader(fs, encode);
                string temp = read.ReadLine();

                StringBuilder sb = new StringBuilder();

                while (temp != null)
                {
                    temp = temp.TrimStart();
                    string reg = @"var+\s*\w+\s*\=+\s*";
                    Regex r = new Regex(reg);

                    if (!temp.Equals(""))
                    {
                        Match m = r.Match(temp);

                        if (m.Value.ToString() != "")
                        {
                            //temp = m.Value.ToString();
                            //temp.Replace("var", "");
                            //temp.Replace("=", "");

                            //wordApp.insertTitle(temp.Trim(), word1.Style.Three, Word.WdColor.wdColorBlack);

                            wordApp.insertText(temp.Trim());
                            wordApp.insertText(sb.ToString());

                            sb.Length = 0;
                        }
                    }

                    if (temp.Trim().Length > 2)
                    {
                        if (temp.TrimStart().Substring(0, 2).Equals("//"))
                        {
                            sb.Append(temp + "");
                        }
                    }

                    temp = read.ReadLine();
                }

                fs.Close();
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                string sourse = this.textBox1.Text.Trim();

                wordApp = new WordApp();
                wordApp.createDocument();

                zhushi_all(sourse);
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button10_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog2 = new OpenFileDialog();
            openFileDialog2.RestoreDirectory = true;
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                this.textBox4.Text = openFileDialog2.FileName;
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button7_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog2 = new OpenFileDialog();
            openFileDialog2.RestoreDirectory = true;
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                this.textBox3.Text = openFileDialog2.FileName;

                wordApp = null;
                string filename = textBox3.Text;
                wordApp = new WordApp();
                wordApp.open(filename);
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            string sourse = this.textBox4.Text;
            wordApp = new WordApp();
            wordApp.createDocument();

            zhushi_cs(sourse);

            //MessageBox.Show("ok");
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sou"></param>
        public void zhushi_cs(string sou)
        {
            FileInfo f2 = new FileInfo(sou);
            if (f2.Extension.Equals(".cs"))
            {
                wordApp.insertTitle(f2.Name, word1.Style.Two, Microsoft.Office.Interop.Word.WdColor.wdColorBrown);

                CsCommand(f2.FullName);
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sourse"></param>
        private void CsCommand(string sourse)
        {
            //得到文件
            Encoding encode = Encoding.Default;
            System.IO.File.SetAttributes(sourse, System.IO.FileAttributes.Normal);
            FileStream fs = new FileStream(sourse, FileMode.Open);

            StreamReader read = new StreamReader(fs, encode);
            string temp = read.ReadLine();

            bool InClass = false;
            StringBuilder sb_cs_zhushi = new StringBuilder();

            //定义正则
            string reg = @".*\b+class\b+.*";
            Regex Regexclass = new Regex(reg);

            string strinterface = @".*\b+interface\b+.*";
            Regex Regexinterface = new Regex(strinterface);

            string strstruct = @".*\b+struct\b+.*";
            Regex Regexstruct = new Regex(strstruct);

            string strenum = @".*\b+enum\b+.*";
            Regex Regexstrenum = new Regex(strenum);

            string strfunction = @"(private|public|protected|void).*\(";
            Regex Regexfunction = new Regex(strfunction);

            //读文件
            while (temp != null)
            {
                temp = temp.Trim();

                if (!temp.Equals("") && temp.Length > 2)
                {
                    if (!temp.TrimStart().Substring(0, 2).Equals("//"))
                    {
                        if (temp.ToLower().IndexOf("class") != -1)
                        {
                            Match m = Regexclass.Match(temp);

                            if (m.Value.ToString() != "")
                            {
                                InClass = true;

                                temp = m.Value.ToString();

                                wordApp.insertTitle(temp, word1.Style.Three, word1.Color.ColorClass);
                                wordApp.insertText(sb_cs_zhushi.ToString());

                                //nei bu
                                temp = read.ReadLine();
                                int count = 0;
                                bool countChange = false;
                                while (temp != null && InClass == true)
                                {
                                    temp = temp.Trim();
                                    if (!temp.Equals("") && temp.Trim().Length > 2)
                                    {
                                        if (temp.TrimStart().Substring(0, 2).Equals("//"))
                                        {
                                            sb_cs_zhushi.Append(temp + "\n");

                                            temp = read.ReadLine();
                                            continue;
                                        }
                                    }

                                    if (temp.IndexOf("{") != -1)
                                    {
                                        count++;
                                        countChange = true;
                                    }
                                    if (temp.IndexOf("}") != -1)
                                    {
                                        count--;
                                    }

                                    if (count == 0 && countChange == true)
                                    {
                                        InClass = false;
                                        break;
                                    }

                                    if (temp.IndexOf("(") != -1)
                                    {
                                        m = Regexfunction.Match(temp);

                                        if (m.Value.ToString() != "")
                                        {
                                            wordApp.insertTitle(temp, word1.Style.Four, word1.Color.ColorFunction);
                                            wordApp.insertText(sb_cs_zhushi.ToString());
                                            sb_cs_zhushi.Length = 0;
                                        }
                                    }

                                    if (temp.ToLower().IndexOf("struct") != -1)
                                    {
                                        m = Regexstruct.Match(temp);

                                        if (m.Value.ToString() != "")
                                        {
                                            temp = m.Value.ToString();

                                            //temp = temp.Substring(0, temp.Length - 1);
                                            wordApp.insertTitle(temp, word1.Style.Four, word1.Color.ColorStruct);
                                            wordApp.insertText(sb_cs_zhushi.ToString());

                                            sb_cs_zhushi.Length = 0;
                                        }
                                    }

                                    if (temp.ToLower().IndexOf("enum") != -1)
                                    {
                                        m = Regexstrenum.Match(temp);

                                        if (m.Value.ToString() != "")
                                        {
                                            temp = m.Value.ToString();

                                            //temp = temp.Substring(0, temp.Length - 1);
                                            wordApp.insertTitle(temp, word1.Style.Four, word1.Color.ColorEnum);
                                            wordApp.insertText(sb_cs_zhushi.ToString());

                                            sb_cs_zhushi.Length = 0;
                                        }
                                    }

                                    temp = read.ReadLine();
                                }

                                wordApp.insertText(sb_cs_zhushi.ToString());

                                sb_cs_zhushi.Length = 0;
                            }
                        }

                        if (temp != null)
                        {
                            if (temp.ToLower().IndexOf("interface") != -1)
                            {
                                Match m = Regexinterface.Match(temp);

                                if (m.Value.ToString() == "")
                                {
                                }
                                else
                                {
                                    temp = m.Value.ToString();

                                    //temp = temp.Substring(0, temp.Length - 1);
                                    wordApp.insertTitle(temp, word1.Style.Three, word1.Color.ColorInterface);
                                    wordApp.insertText(sb_cs_zhushi.ToString());

                                    sb_cs_zhushi.Length = 0;
                                }
                            }

                            if (temp.ToLower().IndexOf("struct") != -1)
                            {
                                Match m = Regexstruct.Match(temp);

                                if (m.Value.ToString() == "")
                                {
                                }
                                else
                                {
                                    temp = m.Value.ToString();

                                    //temp = temp.Substring(0, temp.Length - 1);
                                    wordApp.insertTitle(temp, word1.Style.Three, word1.Color.ColorStruct);
                                    wordApp.insertText(sb_cs_zhushi.ToString());

                                    sb_cs_zhushi.Length = 0;
                                }
                            }

                            if (temp.ToLower().IndexOf("enum") != -1)
                            {
                                Match m = Regexstrenum.Match(temp);

                                if (m.Value.ToString() == "")
                                {
                                }
                                else
                                {
                                    temp = m.Value.ToString();

                                    //temp = temp.Substring(0, temp.Length - 1);
                                    wordApp.insertTitle(temp, word1.Style.Three, word1.Color.ColorStruct);
                                    wordApp.insertText(sb_cs_zhushi.ToString());

                                    sb_cs_zhushi.Length = 0;
                                }
                            }
                        }
                    }
                    else if (temp.Trim().Length > 2)
                    {
                        if (temp.TrimStart().Substring(0, 2).Equals("//"))
                        {
                            sb_cs_zhushi.Append(temp + "\n");
                        }
                    }
                }

                temp = read.ReadLine();
            }

            fs.Close();
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button9_Click(object sender, EventArgs e)
        {
            try
            {
                zhuijia_cs();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
        }
    }
}